package com.qige.dome.homework;

import java.util.*;

/**
 * @Author qige
 * @Date 2024-08-01 19:15
 * @Description
 */
public class homework3 {
    /*
    3.实现List和Map数据的转换。具体要求如下：
    功能1：定义方法public void listToMap( ){ }将List中Student元素封装到Map中
    1)使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息并加入List
    2)遍历List，输出每个Student信息
    3)将List中数据放入Map，使用Student的id属性作为key，使用Student对象信息作为value
    4)遍历Map，输出每个Entry的key和value
    功能2：定义方法public void mapToList( ){ }将Map中Student映射信息封装到List
    1)创建实体类StudentEntry，可以存储Map中每个Entry的信息
    2)使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息，并使用Student的id属性作为key，存入Map
    3)创建List对象，每个元素类型是StudentEntry
    4)将Map中每个Entry信息放入List对象
     */
    public static void main(String[] args) {
        listToMap();
        mapToList();
    }
    public static void listToMap( ){
        Student stu1 = new Student(1, "张三", 18, "男");
        Student stu2 = new Student(2, "李四", 19, "女");
        Student stu3 = new Student(3, "王五", 20, "男");
        Student stu4 = new Student(4, "赵六", 21, "男");
        List<Student> list = Arrays.asList(stu1, stu2, stu3, stu4);
        for (Student student : list) {
            System.out.println(student);
        }
        Map<Integer, Student> map = new HashMap<>();
        for (Student student : list) {
            map.put(student.getId(), student);
        }
        Set<Integer> keySet = map.keySet();
        Iterator<Integer> iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Integer key = iterator.next();
            Student value = map.get(key);
            System.out.println(key + "---" + value);
        }
    }
    public static void mapToList( ){
        Student stu1 = new Student(1, "张三", 18, "男");
        Student stu2 = new Student(2, "李四", 19, "女");
        Student stu3 = new Student(3, "王五", 20, "男");
        Student stu4 = new Student(4, "赵六", 21, "男");
        Map<Integer, Student> map = new HashMap<>();
        map.put(stu1.getId(), stu1);
        map.put(stu2.getId(), stu2);
        map.put(stu3.getId(), stu3);
        map.put(stu4.getId(), stu4);
        List<Student> list = new ArrayList<>();
        Set<Map.Entry<Integer, Student>> entries = map.entrySet();
        for (Map.Entry<Integer, Student> entry : entries) {
            list.add(entry.getValue());
        }
        for (Student student : list) {
            System.out.println(student);
        }
    }
}
class Student{
    private Integer id;
    private String name;
    private Integer age;
    private String sex;

    public Student() {
    }

    public Student(Integer id, String name, Integer age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;
        return Objects.equals(id, student.id) && Objects.equals(name, student.name) && Objects.equals(age, student.age) && Objects.equals(sex, student.sex);
    }

    @Override
    public int hashCode() {
        int result = Objects.hashCode(id);
        result = 31 * result + Objects.hashCode(name);
        result = 31 * result + Objects.hashCode(age);
        result = 31 * result + Objects.hashCode(sex);
        return result;
    }
}